struct ListNode {
  int val;
  ListNode *next;
  ListNode() : val(0), next(nullptr) {}
  ListNode(int x) : val(x), next(nullptr) {}
  ListNode(int x, ListNode *next) : val(x), next(next) {}
};
struct TreeNode {
  int val;
  TreeNode *left;
  TreeNode *right;
  TreeNode() : val(0), left(nullptr), right(nullptr) {}
  TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  TreeNode(int x, TreeNode *left, TreeNode *right)
      : val(x), left(left), right(right) {}
};

class Solution {
  bool proc(ListNode *nowl, TreeNode *nowt) {
    if (nowl == nullptr) {
      return true;
    }
    if (!nowt) {
      return false;
    }
    if (nowl->val != nowt->val) {
    	return false;
    } else {
      return proc(nowl->next, nowt->left) || proc(nowl->next, nowt->right);
    }
  }

public:
  bool isSubPath(ListNode *head, TreeNode *root) {
    if (head == nullptr && root == nullptr) {
      return true;
    }
    if ((!head && root) || (head && !root)) {
      return false;
    }
    if(proc(head,root)){
	return true;
    }
    return isSubPath(head, root->left) || isSubPath(head, root->right);
  }
};
